﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SSN.BusinessFramework.Jobs;
using SSN.Utilities.Security;
using SSN.Utilities.Cache;


namespace SSN.WebFramework
{
    public class JobCache : KeyValueCache<int, Job>
    {
        public JobCache()
            : base(_AllJobs, _GetJob, new TimeSpan(24, 0, 0))
        {
        }

        private static Dictionary<int, Job> _AllJobs()
        {
            Dictionary<int, Job> jobsTable = new Dictionary<int, Job>();
            JobSearch js = new JobSearch(new AnonymousSecurityContext());
            foreach (var j in js.AllJobs())
            {
                jobsTable[j.ID] = j;
            }

            return jobsTable;
        }

        private static Job _GetJob(int id)
        {
            Job j = new Job();
            if (j.Load(new AnonymousSecurityContext(), id))
            {
                return j;
            }

            return null;
        }
    }
}
